//
//  AmuseMuneCollectionViewCell.swift
//  DouYuZB
//
//  Created by 梁森 on 2020/4/25.
//  Copyright © 2020 梁森. All rights reserved.
//

import UIKit

private let kItemWidth:CGFloat = kScreenWidth / 4
private let kItemHeight:CGFloat = 90
private let kCollectionH:CGFloat = 180.0

class AmuseMuneCollectionViewCell: BaseCollectionViewCell {
    var anchorGroups:[AnchorGroup]?{
        didSet{
            self.collectionView.reloadData()
        }
    }
    // MARK: 懒加载
    lazy var collectionView:UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: kItemWidth, height: kItemHeight)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
//        layout.scrollDirection = .horizontal
        let colFrame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kCollectionH)
        let collectionView = UICollectionView(frame: colFrame, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(RecommendGameCell.self, forCellWithReuseIdentifier: RecommendGameCell.reuseIdentifier())
        collectionView.backgroundColor = UIColor.white
        return collectionView
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
// MARK:设置UI
extension AmuseMuneCollectionViewCell{
    fileprivate func setupUI(){
        addSubview(collectionView)
    }
}
// MARK:遵守协议
extension AmuseMuneCollectionViewCell:UICollectionViewDelegate, UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if anchorGroups == nil {return 0}
        return anchorGroups!.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: RecommendGameCell.reuseIdentifier(), for: indexPath) as! RecommendGameCell
        cell.anchorGroup = anchorGroups![indexPath.item]
        cell.backgroundColor = UIColor.randomColor()
        return cell
    }
    
    
}
